package com.task.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.task.bean.UserBean;
import com.task.services.TaskLogService;
import com.task.services.TaskRegistService;
import com.task.services.TaskRunService;
import com.task.util.StringUtil;

@Controller
@RequestMapping
public class UserController {
	
	@Autowired
	private TaskRegistService taskRegistService;
	
	@Autowired
	private TaskRunService taskRunService;
	
	@Autowired
	private TaskLogService taskLogService;
	
	private Logger log=LoggerFactory.getLogger(UserController.class);
	
	@Value("${loginUserName}")
	private String userName;
	
	@Value("${loginPassWord}")
	private String passWord;
	
	@RequestMapping
	public String getLogin() {
		return "login";
	}
	
	@RequestMapping(value="/index")
	public String index() {
		return "index";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> login(@RequestBody UserBean bean,HttpSession session) {
	    log.info("用户："+bean.getUserName()+"在尝试登陆!");
	    
	    if(this.userName.equals(bean.getUserName()) &&
	       this.passWord.equals(bean.getPassWord())) {
	       session.setAttribute("user", bean);
	       return StringUtil.returnSucess();
	    }else {
	       return StringUtil.returnMap("F", "用户名或密码不正确");
	    }
	}
	
	@RequestMapping(value="/logout")
	public String logOut(HttpSession session) {
		session.removeAttribute("user");
		return "login";
	}
	
	@RequestMapping(value="/welcome")
	public String welCome(HttpServletRequest request) {
		Map<String,String> queryMap=new HashMap<String,String>();
		//注册任务
		int regsitCount=this.taskRegistService.getRegistCount(null);
		
		//执行任务
		int runCount=this.taskRunService.getAllTaskRunCount(null);
		
		//正在执行
		queryMap.clear();
		queryMap.put("status", "1");
		int runCountS=this.taskRunService.getAllTaskRunCount(queryMap);
		
		//成功
		queryMap.clear();
		queryMap.put("logStatus", "0");
		int taskCountS=this.taskLogService.getAllTaskLogCount(queryMap);
		
		//失败
		queryMap.clear();
		queryMap.put("logStatus", "-1");
		int taskCountF=this.taskLogService.getAllTaskLogCount(queryMap);
		
		request.setAttribute("regsitCount", regsitCount);
		request.setAttribute("runCount", runCount);
		request.setAttribute("runCountS", runCountS);
		request.setAttribute("taskCountS", taskCountS);
		request.setAttribute("taskCountF", taskCountF);
		return "welcome";
	}
	
	@RequestMapping(value="/welcome1")
	public String welCome1() {
		return "welcome1";
	}
}
